<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Lib\Category;
class NewsController extends CommonController {
    protected $tableName = 'News';

    public function index() {
        $M = M("News");
        $category_id = I('category_id',0);

        $categoryList = D('News')->getCategoryList();
        $adminArr = F('admin_list');
        $statusArr = array('1'=>'已审核', '2'=>'未审核');

		if (IS_GET){
			$keyword = trim($_GET['keyword']);

            if(!empty($category_id)){
                $map['category_id'] = $category_id;
            }

			if(!empty($cidArr)) $map['category_id'] = array('in',$cidArr);
			if(!empty($keyword)) $map['title'] = array('like','%'.$keyword.'%');
		}

        $count = $M->where($map)->count();
        $Page       = new \Think\Page($count,25);
        $showPage = $Page->show();
        $this->assign("page", $showPage);
        $list = $M->where($map)->order('update_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        foreach ($list as $key => $val) {
            $list[$key]['categoryName'] = $categoryList[$val['category_id']]['name'];
            $list[$key]['admin'] = $adminArr[$val['user_id']]['username'];
            $list[$key]['status'] = $statusArr[$val['status']];
        }
        $this->assign('list',$list);
        $this->assign('category_list',$categoryList);
        $this->display();
    }

    public function category() {
        if (IS_POST) {
            echo json_encode(D("News")->category());
        } else {
            $this->assign("list", D("News")->category());
            $this->display();
        }
    }

    public function add() {
        if (IS_POST) {
            $id = I('id');
            if(!$id){
                echo json_encode(D("News")->add());
            }else{
                echo json_encode(D("News")->edit());
            }
        } else {
            $id = I('id');
            $info = array();
            if($id){
                $M = M('News');
                $Content = M('NewsContent');
                $info = $M->find($id);
                $info['content'] = $Content->where('id='.$id)->getField('content');

                if(!empty($info['house_id'])){
                    $info['houseName'] = D('House')->getInfo($info['house_id'],'name');
                }
            }else{
                $info['status'] = 1;
            }
            $this->assign("info", $info);
            $this->assign("list", D("News")->category());
            $this->display();
        }
    }

    public function checkNewsTitle() {
        $M = M("News");
        $where = "title='" . $this->_get('title') . "'";
        if (!empty($_GET['id'])) {
            $where.=" And id !=" . (int) $_GET['id'];
        }
        if ($M->where($where)->count() > 0) {
            echo json_encode(array("status" => 0, "info" => "已经存在，请修改标题"));
        } else {
            echo json_encode(array("status" => 1, "info" => "可以使用"));
        }
    }

	public function detail(){
		$id = $_REQUEST['id'];
		$where['id'] = $id;
		$this->assign("info", D("News")->detail($where));
		$this->display();
	}

    public function del() {
        $News = M("News");
        $ids = I('post.ids');
        $map['id'] = array('in',$ids);

        if ($News->where($map)->delete()) {
            $result = array("status"=>1,"info"=>"成功删除",'url'=>'');
        } else {
            $result = array("status"=>0,"info"=>"删除失败，可能是不存在该ID的记录");
        }
        $this->ajaxReturn($result);
        exit;
    }


	/*微博列表*/
	 public function microblog() {
		import("ORG.Util.Page");       //载入分页类
        $Microblog = M("Microblog");
        $count = $Microblog->where($where)->count();
        $page = new Page($count, 13);
        $showPage = $page->show();
        $this->assign("page", $showPage);
        $this->assign("list", D("News")->microblog($where, $page->firstRow, $page->listRows));
        $this->display();
    }

	/*添加微博*/
	public function addMicroblog() {
		if (IS_POST) {
            echo json_encode(D("News")->addMicroblog());
        } else {
			if(IS_GET){
				$id = $_GET['id'];
				if(!empty($id))
				$this->assign('info',D('News')->getMicroblogInfo($id));
			}
            $this->display();
        }
    }

	/*日记列表*/
	public function diary(){
		import("ORG.Util.Page");       //载入分页类
        $Diary = M("Diary");
        $count = $Diary->where($where)->count();
        $page = new Page($count, 20);
        $showPage = $page->show();
        $this->assign("page", $showPage);
        $this->assign("list", D("News")->Diary($where, $page->firstRow, $page->listRows));
        $this->display();
	}

	/*添加日记*/
	public function addDiary(){
		if (IS_POST) {
            echo json_encode(D("News")->addDiary());
        } else {
			$this->assign('weatherArr',D("News")->getWeather());
			if(IS_GET && !empty($_GET['id'])){
				$id = $_GET['id'];
				$this->assign('info',$this->info('Diary'));
				$this->assign('actionName','添加日记');
			}else{
				$this->assign('actionName','编辑日记');
			}
            $this->display();
        }
	}

	/*删除日记*/
	public function delDiary(){
		if (M("Diary")->where("id=".(int)$_GET['id'])->delete()) {
            $this->ajaxReturn(1,"成功删除",1);
        } else {
			 $this->ajaxReturn(0,"删除失败，可能是不存在该ID的记录",0);
        }
	}

	/*删除日记*/
	public function showDiary(){
		$this->assign('info',$this->info('Diary'));
		$this->display('detail');
	}

	public function changeNewsStatus(){
		$statusId = $_GET['statusId'];
		$id = $_GET['id'];
		if(empty($id)) return false;
		if($statusId==1){
			$data['status'] = 0;
			$info = "待审核";
		}else{
			$data['status'] = 1;
			$info = "审核通过";
		}
		M('News')->where('id='.$id)->save($data);
		$this->ajaxReturn($data['status'],$info,1);
	}

	/* 记事本列表 */
	public function notepad(){
		import("ORG.Util.Page");       //载入分页类
        $M = M("Notepad");
        $count = $M->where($where)->count();
        $page = new Page($count, 20);
        $showPage = $page->show();
        $this->assign("page", $showPage);
        $this->assign("list", D("News")->notepad($where, $page->firstRow, $page->listRows));
        $this->display();
	}

	/* 添加记事本 */
	public function addNotepad(){
		if (IS_POST) {
            echo json_encode(D("News")->addNotepad());
        } else {
			if(IS_GET && !empty($_GET['id'])){
				$this->assign('info',$this->info('Notepad'));
				$this->assign('actionName','添加记事本');
			}else{
				$this->assign('actionName','编辑记事本');
			}
            $this->display();
        }
	}

}